﻿/* Muaz Khan – http://dashingquill.web.officelive.com */

/***********************************
* Login or Sign-up related all JavaScript stuff goes here
***********************************/

var account = window.account || {};
account = (function () {

    /***********************************
    * Using dashingquill and Sizzle libraries for cross-browser JSON helps + DOM searchers
    ***********************************/
    var dq = dashingquill;
    var sz = Sizzle;

    /***********************************
    * Create New Account
    ***********************************/
    var createAccount = function () {
        dq.bind('#create-account', 'click', function (createAccountButton) {
            createAccountButton.innerHTML = 'Creating Account...';

            var fields = sz('#username, #fullname, #password, #email');
            if (!validate(fields)) {
                createAccountButton.innerHTML = 'Create Account';
                return;
            }
            var data = '?userName=' + fields[0].value + '&fullName=' + fields[1].value + '&password=' + fields[2].value + '&gravatar=' + gravatar(fields[3].value);
            dq.xhr({
                url: dq.domainName + '/CreateAccount' + data,
                callback: function (response) {
                    if (response.IsSuccess === true) window.location = dq.domainName + '/' + response.token;
                    else {
                        var error = dq.find('#error2');
                        dq.show(error);
                        error.innerHTML = response.Error;
                        createAccountButton.innerHTML = '+ Create Account';
                    }
                }
            });
        });
    };

    /***********************************
    * Login to DTweet
    ***********************************/
    var login = function () {
        dq.bind(sz('#login-name, #login-password'), 'keyup', function (elem, e) {
            if (elem.value.length <= 0) return;
            if (e.keyCode == 13) {
                LogInTheUser(dq.find('#login-now'));
            }
        });
        dq.bind('#login-now', 'click', function (loginButton) {
            LogInTheUser(loginButton);
        });
        var LogInTheUser = function (loginButton) {
            loginButton.innerHTML = 'Logging in...';

            var fields = sz('#login-name, #login-password');
            if (!validate(fields)) {
                loginButton.innerHTML = 'Log in &gt;&gt;';
                return;
            }

            var data = '?userName=' + fields[0].value + '&password=' + fields[1].value;
            dq.xhr({
                url: dq.domainName + '/loginRequest' + data,
                callback: function (response) {
                    if (response.success == true) window.location = dq.domainName + '/' + response.token;
                    else {
                        var error = dq.find('#error');
                        dq.show(error);
                        error.innerHTML = response.error;
                        loginButton.innerHTML = 'Log in &gt;&gt;';
                    }
                }
            });
        };
    };

    /***********************************
    * This is the function that handles all events on this page!
    ***********************************/
    var handleEvents = function () {
        createAccount();
        login();

        /***********************************
        * Getting Gravatar image from the entered Email address
        * Note: We are only saving gravatar URL, we never save email.
        ***********************************/
        dq.bind('#email', 'keyup', function (email) {
            var gravatarImage = dq.find('#gravatar-image');
            if (gravatarImage) {
                gravatarImage.src = gravatar(email.value);
                dq.show(gravatarImage);
            }
        });

        /***********************************
        * User Name must be accurate username!! -- auto generating from full-name
        ***********************************/
        dq.bind('#fullname', 'keyup', function (fullName) {
            dq.find('#username').value = resolveForUserName(fullName.value);
        });

        /***********************************
        * User Name must be accurate username!! -- auto generating from full-name
        ***********************************/
        dq.bind('#username', 'keyup', function (username) {
            dq.find('#username').value = resolveForUserName(username.value);
        });

        /***********************************
        * Only hyphen (_) or underscore (-) are allowed in the username
        ***********************************/
        var resolveForUserName = function (value) {        
            return value.replace(/(\(|\)|\,|\s|\'|\"|\\|\[|\]|{|}|\||\\|\!|@|#|\$|%|\^|\*|~|`|'|"|\?|<|>|\.|=|\+|&|\/)/g, '');
        }
    };

    /***********************************
    * Validating fields for empty
    ***********************************/
    var validate = function (fields) {
        dq.each(fields, function (field) {
            if (field.value.length <= 0) {
                field.style.borderColor = 'red';
                return false;
            }
            else field.style.borderColor = '#DCDCDC';
        });
        return true;
    };

    /***********************************
    * And here we goes to return public stuff because closures are --- !!! :) :) -- private
    ***********************************/
    return {
        handleEvents: function () {
            return handleEvents();
        }
    };
})(account);